﻿/*----------------------------------------------------
 * Author : Tharinduni Udugamasuriya
 * FinCon dll for Handeling DB 
 * User Control to write DB settings in to Registry
 ---------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;

namespace DSPL.Common
{
    /// <summary>
    /// Interaction logic for DBConnector.xaml
    /// </summary>
    public partial class DBConnector : UserControl
    {
        DBConnection objdbCls = new DBConnection();
        RegistryManager objFinCon = new RegistryManager();
        IMessageService messageService;

        public DBConnector()
        {
            IUnityContainer container = GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                messageService = container.Resolve<IMessageService>();
            } 
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Mouse.OverrideCursor = Cursors.Wait;
                DBList objdb = new DBList();
                objdb.ShowDialog();
                if (objdb.DBEngine.Trim() != "")
                {
                    txtServer.Text = objdb.DBEngine;
                }
                Mouse.OverrideCursor = null;
            }
            catch (Exception)
            {
                messageService.ShowMessage(MessageText.ServerSelectingError, MessageType.ErrOK);
            }
        }

        private void setDbList(DataTable dt)
        {
            try
            {
                cmbDatabase.DataContext = dt;
            }
            catch (Exception ex)
            {
                messageService.ShowException(ex.Message.ToString(), new StackTrace().GetFrame(1).GetMethod().Name);
            }
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                setDbList(objdbCls.getDbList(txtServer.Text, txtUser.Text, txtPassword.Password));
            }
            catch (Exception)
            {
                messageService.ShowMessage(MessageText.DataBaseLoadingError, MessageType.ErrOK);
            }
        }

        private void txtPassword_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                setDbList(objdbCls.getDbList(txtServer.Text, txtUser.Text, txtPassword.Password));
            }
            catch (Exception)
            {
                messageService.ShowMessage(MessageText.DataBaseLoadingError, MessageType.ErrOK);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            GC.Collect();
            //(this.Parent as StackPanel).Children.Remove(this);
            Window parentwin = Window.GetWindow(this);
            parentwin.Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (objdbCls.ConnectionSuccess(txtServer.Text, txtUser.Text, txtPassword.Password, cmbDatabase.SelectedValue.ToString()))
                {
                    string constr = objdbCls.BuildConnectionString(txtServer.Text, txtUser.Text, txtPassword.Password, true, cmbDatabase.SelectedValue.ToString());
                    if (objFinCon.SaveDbConnection(txtServer.Text, txtUser.Text, txtPassword.Password, cmbDatabase.SelectedValue.ToString(), constr))
                    {
                        messageService.ShowMessage(MessageText.DataBaseSavingSuccess, MessageType.InfoOK);
                        //(this.Parent as StackPanel).Children.Remove(this);
                        Window parentwin = Window.GetWindow(this);
                        parentwin.Close();
                    }
                    else
                    {
                        messageService.ShowMessage(MessageText.DataBaseSavingError, MessageType.WarningOK);
                    }
                }
                else
                {
                    messageService.ShowMessage(MessageText.DataBaseSavingError, MessageType.WarningOK);
                    //(this.Parent as StackPanel).Children.Remove(this);
                }
            }
            catch (Exception ex)
            {
                messageService.ShowException(ex.Message.ToString(), new StackTrace().GetFrame(1).GetMethod().Name);
            }
        }
    }
}
